module Sim.CRayTrace ( Trace(..), genCRTraces )  where

import    Control.Monad ( replicateM )
import    System.Random ( randomRIO )
import       Sim.Helper ( unsafeSequenceIO )

type Area  = (((Int,Int), (Int,Int)), Double)

-- След от космической частицы

data Trace = Trace (Double,Double) (Double,Double) Double

-- Генерация следов от космических частиц для заданного их количества и площади

genCRTraces :: Area -> IO [Trace]
genCRTraces a = do
   unsafeSequenceIO $ repeat $ genCTrace a

genCTrace :: Area -> IO Trace
genCTrace (((x,y),(x',y')),t) = do
    x0 <- randomRIO $ (fromIntegral x, fromIntegral x') :: IO Double
    y0 <- randomRIO $ (fromIntegral y, fromIntegral y') :: IO Double
    az <- randomRIO (0.0, 2*pi)
    fi <- randomRIO'

    let l  = t * tan fi
        dx = l * cos az
        dy = l * sin az

    return (Trace (x0,y0) (x0+dx, y0+dy) fi)

    where
       randomRIO' = do
          x <- randomRIO (0.0, pi/2)
          y <- randomRIO (0.0, 1.0 )
          if y < sin (2*x)
             then return x
             else randomRIO'
